import util from '../../utils/util'
import { EnumUserType } from '../../utils/constant.js'
const app = getApp(),url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    success:false,
    blurts:false,
    clurts:false,
    plurts:false,
    mmlurts:false,
    phone:'',
    pass:'',
    ciid:'',
    account:'',
    sfzZm:'',
    sfzFm:'',
    wxName: '',
    phone_te:'',//手机号   
    bidndtorf:false,//判断允不允许输入 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.phone_tel != undefined){
      that.setData({
        phone_te:options.phone_tel,//显示
        phone:options.phone_tel,//提交
        bidndtorf:true,
      })
    }
    that.setData({
      openid:options.openid,
      type:options.type
    })
    // wx.getUserInfo({
    //   success: res => {
    //     that.userInfo = res.userInfo
    //     wx.setStorageSync('userInfo', res.userInfo)
    //   }
    // })
  },
  phone(e){
    var that = this    
    that.setData({
      phone : e.detail.value.replace(/\s+/g,""),
    });
  },
  codeID(e){
    var that = this
    that.setData({
      ciid : e.detail.value.replace(/\s+/g,""),
      // success:true,
    });
  },
  // 账号 用户名 
  phoneinput(e){
    var that = this
    that.setData({
      account : e.detail.value.replace(/\s+/g,""),
    });
  },
  passeqw(e){
    var that = this
    that.setData({
      pass : e.detail.value.replace(/\s+/g,""),
    });
  },
  chooseImage: function (e) {
    console.log(e)
    let {key} = e.currentTarget.dataset
    var that = this;
    wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
        if(res.tempFilePaths.length > 0){
          that.setData({[key]:res.tempFilePaths[0]})
          that.uploadFile(key, res.tempFilePaths[0])
        }
      }
    })
  },
  uploadFile: function(key, tempUrl){
    var that = this
    wx.uploadFile({
      url:url+'cvBaseInfo/uploadCVPhoto',
      filePath: tempUrl,
      name: 'cv_photo',
      success: function (res) {
        var ret=JSON.parse(res.data)
        if (ret.code == 200) {
            that.setData({[key]:ret.data})
            wx.showToast({
              title: ret.msg,
              icon:'none',
            })
        }
      }
    });
  },
  bindDo(){
    var that = this
    let userInfo = wx.getStorageSync('userInfo')
    var openid = that.data.openid
    var phone = that.data.phone
    var password = that.data.pass
    var type = that.data.type
    var ciid = that.data.ciid
    var account = that.data.account//账号
    var nickName = userInfo.nickName
    let sfzZm = this.data.sfzZm
    let sfzFm = this.data.sfzFm

    if(!/^1\d{10}$/.test(phone)){
			wx.showModal({
				title: '友情提示',
				content: '请输入正确的手机号',
				showCancel: false,
			});
			return false;
		}
    if(account == ''){
      wx.showToast({
        title: '请填写账号',
        icon:'none',
      })
      return false
    }
    if(!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test(password)){
			wx.showToast({
				title: '请输入6-16位数字字母组合密码',
				icon:'none',
			})
			return false
    }
    if(!sfzZm){
      wx.showToast({
        title: '请上传身份证正面照',
        icon:'none',
      })
      return false
    }
    if(!sfzFm){
      wx.showToast({
        title: '请上传身份证反面照',
        icon:'none',
      })
      return false
    }
    var mima = util.strEncode(password)
    let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    if(!_IDRe18.test(ciid)){
      wx.showToast({
        title: '身份证号输入有误',
        icon:'none',
      })
			return false;
    }
    let formData = {
      approval: '1',
      open_id:openid,
      nickName: nickName,
      phone:phone,
      ciid:ciid,
      sfzZm,
      sfzFm,
      account:account,
      password:mima,//mima password
    }

    if(type === 'rereg'){
      this.deleteUser(formData)
      return
    }
    this.userReg(formData)
    
  },
  deleteUser(formData){
    let userMessage = wx.getStorageSync('user_massage')
    let _this = this
    wx.request({
      url: url+'enterprise/deleteUserMassage',
      method: "GET",
      data:{
        delete_id: userMessage.id
      },
      success(res){
        var ret = res.data
        if (ret.code !== 200) {
          wx.showToast({
            title: res.message || '用户信息注销失败！',
            icon:'none'
          })
          return
        }
        _this.userReg(formData)
      }
    })
  },
  userReg(formData){
    wx.request({
      header: {
        'content-type': 'application/json' // 默认值
      },
      url:url+'wx_commen/up_user_massage_by_open_id',
      method: "GET",
      data:formData,
      success:function(res){
        var ret = res.data
        if(res.data.code == 'success'){
          wx.setStorageSync('user_massage', res.data.massage)
          wx.setStorageSync('phone', res.data.massage.mobileNum)
          wx.setStorageSync('userType', EnumUserType.GeRen)
					
          wx.showToast({
            title: '设置成功',
            icon:'none'
          })
          setTimeout(function(){
            wx.switchTab({
              url: '/pages/index/index',
            })
          },1500)
        }else if(res.data.code == 'error'){
          wx.showToast({
            title: '绑定手机号有误',
            icon:'none'
          })
          return false
        }else{
          wx.showToast({
            title: res.data.massage,
            icon:'none'
          })
        }
      }
    })
  },
	nameOnBlur(e){
		var that = this
		var blur = e.detail.value
		if(blur == ''){
			console.log('1')
			that.setData({blurts:true})
		}else{
			that.setData({blurts:false})
		}
	},

	codeOnBlur(e){
		var that = this
		var clur = e.detail.value
		if(clur == ''){
			console.log('1')
			that.setData({clurts:true})
		}else{
			that.setData({clurts:false})
		}
  },
  
  phoneOnBlur(e){
		var that = this
		var phone = e.detail.value
		if(phone == ''){
			console.log('1')
			that.setData({plurts:true})
		}else{
			that.setData({plurts:false})
		}
  },
  
  passOnBlur(e){
		var that = this
		var pass = e.detail.value
		if(pass == ''){
			console.log('pass')
			that.setData({mmlurts:true})
		}else{
			that.setData({mmlurts:false})
		}
  },
  
})